<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="US-ASCII" omit-xml-declaration="yes" />

<!-- 
	Subversion RSS Generation Transform
	 by Greg
	 
	This transform will take the output from 
	 svn log -v -r low:high [two dashes]xml "repo/path"
	and a XML file named "feedinfo.xml" with 
	
	 <info>
		<title>title</title>
		<link>link</link>
		<description>descriptive text</description>
		<language>en-us</language>
		<generator>Subversion RSS Generator v1.0</generator>
		<imgUrl>url to image</imgUrl>
		<imgTitle>image title</imgTitle>
		<imgLink>where clicking on the image will take you</imgLink>
		<repoLink>url to repository</repoLink>
	 </info>
	
	and produce a RSS Feed with the log messages

-->

<!-- Globals -->
<!-- Grab info from external xml file -->
<xsl:variable name="repoPath"><xsl:value-of select="document('feedinfo.xml')/info/repoLink"/></xsl:variable>
<xsl:variable name="title"><xsl:value-of select="document('feedinfo.xml')/info/title"/></xsl:variable>
<xsl:variable name="link"><xsl:value-of select="document('feedinfo.xml')/info/link"/></xsl:variable>
<xsl:variable name="description"><xsl:value-of select="document('feedinfo.xml')/info/description"/></xsl:variable>
<xsl:variable name="language"><xsl:value-of select="document('feedinfo.xml')/info/language"/></xsl:variable>
<xsl:variable name="generator"><xsl:value-of select="document('feedinfo.xml')/info/generator"/></xsl:variable>
<xsl:variable name="imgUrl"><xsl:value-of select="document('feedinfo.xml')/info/imgUrl"/></xsl:variable>
<xsl:variable name="imgTitle"><xsl:value-of select="document('feedinfo.xml')/info/imgTitle"/></xsl:variable>
<xsl:variable name="imgLink"><xsl:value-of select="document('feedinfo.xml')/info/imgLink"/></xsl:variable>

<xsl:template match="log">
	<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
		<channel>
		
		<!-- RSS Feed information -->
		<title><xsl:value-of select="$title"/></title>
		<link><xsl:value-of select="$link"/></link>
		<description><xsl:value-of select="$description"/></description>
		<language><xsl:value-of select="$language"/></language>
		<generator><xsl:value-of select="$generator"/></generator>
		<image>
			<url><xsl:value-of select="$imgUrl"/></url>
			<title><xsl:value-of select="$imgTitle"/></title>
			<link><xsl:value-of select="$imgLink"/></link>
		</image>
		
		<xsl:for-each select="logentry">
		
		<!-- For each item that will be displayed -->
		<item>
			<!-- Add the revision to the repo path --> 
			<xsl:variable name="repoPathWithRev">
				<xsl:call-template name="replace-string">
					<xsl:with-param name="text" select="$repoPath"/>
					<xsl:with-param name="from"><xsl:text>#revision#</xsl:text></xsl:with-param>
					<xsl:with-param name="to" select="@revision"/>
				</xsl:call-template>
			</xsl:variable>
			
			<!-- Link to the source -->
			<xsl:variable name="sourcePathWithRev">
				<xsl:call-template name="replace-string">
					<xsl:with-param name="text" select="$repoPathWithRev"/>
					<xsl:with-param name="from"><xsl:text>!tree</xsl:text></xsl:with-param>
					<xsl:with-param name="to"><xsl:text>!source</xsl:text></xsl:with-param>
				</xsl:call-template>
			</xsl:variable>
			
			<!-- 
				Generate the title
				The format is "Rev - Author - First 25 chars of log message"
				If the message is longer than 25 chars we add ... to the end
				If the message doesn't contain a message (because someone is a 
				jackass) then we won't show the " - Message" part
			-->
			<title>
				<xsl:value-of select="@revision"/>
				<xsl:text> - </xsl:text>
				<xsl:value-of select="author"/>
				
				<xsl:choose>
					<!-- No Message -->
					<xsl:when test="string-length(msg) = 0">
						
					</xsl:when>
					
					<!-- Short Message -->
					<xsl:when test="string-length(msg) &lt; 25">
						<xsl:text> - </xsl:text>
						<xsl:value-of select="msg"/>
					</xsl:when>
					
					<!-- Long Message -->
					<xsl:otherwise>
						<xsl:text> - </xsl:text>
						<xsl:value-of select="substring(msg,1,25)"/>
						<xsl:text> ...</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</title>
			
			<!-- Body -->
			<description>
			
				<!-- If we have a log message display it -->
				<xsl:if test="string-length(msg) &gt; 0">
					<xsl:text>&lt;b&gt;Log Message:&lt;/b&gt;&lt;br /&gt;</xsl:text>
					<xsl:call-template name="replace-string">
						<xsl:with-param name="text" select="msg"/>
						<xsl:with-param name="from" select="'&#10;'"/>
						<xsl:with-param name="to" select="'&lt;br /&gt;'"/>
					</xsl:call-template>
					<!--<xsl:value-of select="msg"/> -->
					
					<xsl:text>&lt;br /&gt;&lt;br /&gt;</xsl:text>
				</xsl:if>
				
				<!-- Adds -->
				<xsl:call-template name="changes">
					<xsl:with-param name="changeName">Added</xsl:with-param>
					<xsl:with-param name="changeType">A</xsl:with-param>
					 <xsl:with-param name="revisionPath"><xsl:value-of select="$sourcePathWithRev" /></xsl:with-param>
				</xsl:call-template>
				
				<!-- Mods -->
				<xsl:call-template name="changes">
					<xsl:with-param name="changeName">Modified</xsl:with-param>
					<xsl:with-param name="changeType">M</xsl:with-param>
					<xsl:with-param name="revisionPath"><xsl:value-of select="$sourcePathWithRev" /></xsl:with-param>
				</xsl:call-template>
				
				<!-- Dels -->
				<xsl:call-template name="changes">
					<xsl:with-param name="changeName">Deleted</xsl:with-param>
					<xsl:with-param name="changeType">D</xsl:with-param>
					<xsl:with-param name="revisionPath"><xsl:value-of select="$sourcePathWithRev" /></xsl:with-param>
				</xsl:call-template>
				
				<!-- Comment to shaim-dev -->
				<xsl:text>&lt;br /&gt;</xsl:text>
				<xsl:text>&lt;a href="mailto:shaim-dev@googlegroups.com?subject=Re: svn commit: r</xsl:text>
				<xsl:value-of select="@revision"/>
				<xsl:text>"&gt;Comment on this revision&lt;/a&gt;</xsl:text>
				
			</description>
			
			<!-- Committer -->
			<dc:creator><xsl:value-of select="author"/></dc:creator>
			<!-- Date Committed -->
			<dc:date><xsl:value-of select="date" /></dc:date>
			<!-- 
				A unique id.
				ie: https://shaim.bountysource.com/svn/!tree/474
			-->
			<guid><xsl:value-of select="$repoPathWithRev" /></guid>
		</item>
		
		</xsl:for-each>
		
		</channel>
	</rss>

</xsl:template>

<xsl:template name="changes">
	<xsl:param name="changeName" />
	<xsl:param name="changeType" />
	<xsl:param name="revisionPath" />
		
		<!-- if we even need to display anything -->
		<xsl:if test="count(paths/path[@action = $changeType]) &gt; 0">
			
			<!-- Heading -->
			<xsl:text>&lt;b&gt;</xsl:text>
			<xsl:value-of select="$changeName"/>
			<xsl:text>&lt;/b&gt;:</xsl:text>
			
			<!-- Generate List -->
			<xsl:text>&lt;ul&gt;</xsl:text>
			
			<xsl:choose>
			
			<!-- We don't need links for deletes cause they aint there no more -->
			<xsl:when test="$changeType = 'D'">
				<xsl:for-each select="paths/path[@action = $changeType]">
					
					<!-- List Item -->
					<xsl:text>&lt;li&gt;</xsl:text>
					<xsl:value-of select="."/>
					<xsl:text>&lt;/li&gt;</xsl:text>
					
				</xsl:for-each>
			</xsl:when>
			
			<!-- Everything else -->
			<xsl:otherwise>
				<xsl:for-each select="paths/path[@action = $changeType]">
					
					<!-- List Item -->
					<xsl:text>&lt;li&gt;&lt;a href='</xsl:text>
					<xsl:value-of select="$revisionPath" />
					<xsl:value-of select="."/>
					<xsl:text>'&gt;</xsl:text>
					<xsl:value-of select="."/>
					<xsl:text>&lt;/a&gt;&lt;/li&gt;</xsl:text>
					
				</xsl:for-each>
				
			</xsl:otherwise>
			</xsl:choose>
			
			<!-- End list -->
			<xsl:text>&lt;/ul&gt;</xsl:text>
		</xsl:if>
		
</xsl:template>

<!-- 
	reusable replace-string function 
	http://aspn.activestate.com/ASPN/Cookbook/XSLT/Recipe/65426
-->
 <xsl:template name="replace-string">
	<xsl:param name="text"/>
	<xsl:param name="from"/>
	<xsl:param name="to"/>
	
	<xsl:choose>
		<xsl:when test="contains($text, $from)">
		
			<xsl:variable name="before" select="substring-before($text, $from)"/>
			<xsl:variable name="after" select="substring-after($text, $from)"/>
			<xsl:variable name="prefix" select="concat($before, $to)"/>
			
			<xsl:value-of select="$before"/>
			<xsl:value-of select="$to"/>
			<xsl:call-template name="replace-string">
				<xsl:with-param name="text" select="$after"/>
				<xsl:with-param name="from" select="$from"/>
				<xsl:with-param name="to" select="$to"/>
			</xsl:call-template>
		</xsl:when> 
		<xsl:otherwise>
			<xsl:value-of select="$text"/>
		</xsl:otherwise>
	</xsl:choose>
 </xsl:template>

</xsl:stylesheet>